﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using QASPRibbon.Commands;

namespace QASPRibbon.Helpers
{
    internal class XmlHelper
    {
        #region Singleton
        private static readonly XmlHelper Instance = new XmlHelper();

        public static XmlHelper Current
        {
            get { return Instance; }
        }

        private XmlHelper()
        {
        }
        #endregion


        internal string GetCommandUIExtensionXML(string definitionsXml, string commandXml, string templatesXml)
        {
            var parsedElements = ParseXml(definitionsXml);
            var commandElements = ParseXml(String.IsNullOrEmpty(commandXml) ? "<root></root>" : commandXml);
            var templateElements = ParseXml(String.IsNullOrEmpty(templatesXml) ? "<root></root>" : templatesXml);

            var document = new XDocument(
                new XElement("CommandUIExtension",
                    new XElement("CommandUIDefinitions")
                    )
                );

            document.Element("CommandUIExtension").Element("CommandUIDefinitions").Add(parsedElements);

            foreach (var templateElement in templateElements)
            {
                document.Element("CommandUIExtension").Element("CommandUIDefinitions").Add(
                    new XElement("CommandUIDefinition",
                        new XAttribute("Location", "Ribbon.Templates._children"),
                        templateElement));
            }

            if (!String.IsNullOrEmpty(commandXml))
                document.Element("CommandUIExtension").Add(
                    new XElement("CommandUIHandlers",
                        commandElements
                        ));


            return document.ToString();
        }

        internal string GetCommandUIDefinitionXML(string location, string definitionXml)
        {
            if (String.IsNullOrEmpty(definitionXml))
                return new XDocument(
                    new XElement("CommandUIDefinition",
                        new XAttribute("Location", location))).ToString();

            var parsedElements = ParseXml(definitionXml);

            var document = new XDocument(new XElement("root"));

            foreach (var parsedElement in parsedElements)
            {
                document.Element("root").Add(
                    new XElement("CommandUIDefinition",
                        new XAttribute("Location", location),
                        parsedElement
                    ));
            }

            return document.ToString();
        }

        internal string GetCommandUIHandlerXML(RibbonCommand command)
        {
            return new XDocument(
                new XElement("CommandUIHandler",
                    new XAttribute("Command", command.ControlId + command.CommandId),
                    new XAttribute("CommandAction", "javascript: " + command.ActionScript),
                    new XAttribute("EnabledScript", "javascript: " + command.EnabledScript))).ToString();
        }

        #region Private methods

        private static IEnumerable<XElement> ParseXml(string xml)
        {
            var parsedElements = XDocument.Parse(xml).Elements();
            if (parsedElements.First().Name == "root")
                parsedElements = parsedElements.First().Elements();
            return parsedElements;
        }

        #endregion

    }
}
